#coding:utf-8
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from flask import Flask, request, render_template
import resolve
msgsrv = resolve.MessageServer()
app = Flask(__name__)
isOnces=True
@app.route('/')
def index():
    #return render_template('message.html')
    return #暂时没用
@app.route('/websocket')
def message():
    if request.environ.get('wsgi.websocket'):
        ws = request.environ['wsgi.websocket']
        print request.environ
        resolve.observers.append(ws)#所有在线用户
        while True:
            message = ws.receive()
            print ("Web->::"+message)
            if message:
                message = json.loads(message)
                if isOnces:#这个玩意并不适用，后期要改！！！！！！！！！！！！！！！！！！！！记住要改！！！！
                    #判断是否第一次运行，是的话就解析引擎
                    game = resolve.Script(message["dat"])
                    isOnces = False
                json_Return = game.Return(resolve.__decodeJsonToDict(message))
                msgsrv.Return_To_User(ws,json_Return)#返回进度

    return "Connected!"
if __name__ == '__main__':
    #http_server = WSGIServer(('0.0.0.0',8000), app, handler_class=WebSocketHandler)
    #http_server.serve_forever()
    obj = resolve.Script("C:\\Users\\bakabie\\Desktop\\TEMP\\nscript\\b.txt")
    #a={'%a':1,"cs":"CA_3"}
    print obj.Return_({"%a":"1","cs":"scene01"}).encode("utf-8")
